package com.huadi.firematerialident.service;

import com.alibaba.fastjson.JSON;
import com.huadi.firematerialident.dao.UserDaoImpl;
import com.huadi.firematerialident.po.NormalUser;
import com.huadi.firematerialident.po.User;
import com.huadi.firematerialident.dao.NormalUserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserDaoImpl userDao;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NormalUserDaoImpl normalUserDao;
    @Override
    public void addUser(User user) {
        userDao.addUser(user.getUsername(),user.getPassword(),user.getRole());
    }

    @Override
    public void delUser() {
        userDao.delUser();
    }

    @Override//获取用户角色（权限）
    public String getRole() {
        return userDao.getRole();
    }

    @Override//用户登录逻辑
    public int login(String username, String password) {
        //信息对比
        // 0、登录失败
        // 1、普通用户
        // 2、鉴定员
        // 3、审查员
        // 4、管理员
        //normaluser
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select NUserCount,NPassword from normaluser");
        for(int i=0;i<list.size();i++){
            if(username.equals((String)list.get(i).get("NuserCount"))&&password.equals((String)list.get(i).get("NPassword"))){
                return 1;
            }
        }
        //identifier
        list = jdbcTemplate.queryForList("select IdentCount,IdentPassword from identifier");
        for(int i=0;i<list.size();i++){
            if(username.equals((String)list.get(i).get("IdentCount"))&&password.equals((String)list.get(i).get("IdentPassword"))){
                return 2;
            }
        }
        //reviewer
        list = jdbcTemplate.queryForList("select RevCount,RevPassword from reviewer");
        for(int i=0;i<list.size();i++){
            if(username.equals((String)list.get(i).get("RevCount"))&&password.equals((String)list.get(i).get("RevPassword"))){
                return 3;
            }
        }
        //admin
        list = jdbcTemplate.queryForList("select AdminCount,AdminPassword from admin");
        for(int i=0;i<list.size();i++){
            if(username.equals((String)list.get(i).get("AdminCount"))&&password.equals((String)list.get(i).get("AdminPassword"))){
                return 4;
            }
        }
        return 0;
    }

    @Override//用户注册逻辑
    public int register(String username, String fPassword, String sPassword) {
        //0：两次输入密码不一致
        //1：账号已存在
        //2：注册成功
        //3:有部分信息没有填写
        int code=0;
        //部分信息没有填写，返回code=3进行提示
        if(username.equals("")||fPassword.equals("")||sPassword.equals("")){
            code = 3;
            return code;
        }
        //第一次第二次密码不相同，返回code=0进行提示
        if(!fPassword.equals(sPassword)) {
            code = 0;
            return code;
        }
        else{
            //信息对比
            // 1、普通用户
            // 2、鉴定员
            // 3、审查员
            // 4、管理员
            List<Map<String,Object>> list = jdbcTemplate.queryForList("select NUserCount,NPassword from normaluser");
            for(int i=0;i<list.size();i++) {
                if (username.equals((String) list.get(i).get("NuserCount"))) {
                    code = 1;
                    return code;
                }
            }
            //identifier
            list = jdbcTemplate.queryForList("select IdentCount,IdentPassword from identifier");
            for(int i=0;i<list.size();i++) {
                if (username.equals((String) list.get(i).get("IdentCount"))) {
                    code = 1;
                    return code;
                }
            }
            //reviewer
            list = jdbcTemplate.queryForList("select RevCount,RevPassword from reviewer");
            for(int i=0;i<list.size();i++) {
                if (username.equals((String) list.get(i).get("RevCount"))) {
                    code = 1;
                    return code;
                }
            }
            //admin
            list = jdbcTemplate.queryForList("select AdminCount,AdminPassword from admin");
            for(int i=0;i<list.size();i++) {
                if (username.equals((String) list.get(i).get("AdminCount"))) {
                    code = 1;
                    return code;
                }
            }
        }
        code=2;
        //验证成功，注册新的普通用户
        if (code==2){
            NormalUser nuser=new NormalUser();
            nuser.setNUserID();
            nuser.setNUserCount(username);
            nuser.setNPassword(fPassword);
            normalUserDao.addNormalUser(nuser);
        }

        return code;
    }

    @Override
    public void logout() {
        userDao.delUser();
    }

    @Override
    public String ListToJSON(int count, List<Map<String, Object>> list) {
        String str = JSON.toJSONString(list);
        return "{\"code\":0,\"msg\":\"\",\"count\":"+count+",\"data\":"+str+"}";
    }

    @Override
    public String getUsername() {
        return userDao.getUsername();
    }
}
